package view;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.BorderLayout;
import java.awt.Desktop.Action;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JToolBar;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import model.kwalificatiebeheer.KwalificatieDossier;

import controller.MainFrameActions;

public class MainFrame extends JFrame{

	private JPanel mainPanel;
	private ConsolePanel consolePanel;
	
	public MainFrame(){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		boolean console = false;
		initGUI(console);
	}
	
	/**
	 * Getters And Setters
	 */
	public JPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(JPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	public ConsolePanel getConsolePanel() {
		return consolePanel;
	}
	
	public void setConsolePanel(ConsolePanel consolePanel) {
		this.consolePanel = consolePanel;
	}

	/**
	 * @name: 		initGui
	 * @description:Init Graphical User Interface
	 * @return: 	void
	 */
	private void initGUI(boolean console){
		setTitle("StudyFlow");
		ImageIcon icon = new ImageIcon(this.getClass().getResource("../resources/favicon.png"));
		setIconImage(icon.getImage());
		setSize(1200, 1000);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		setExtendedState(Frame.MAXIMIZED_BOTH);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{484, 0};
		gridBagLayout.rowHeights = new int[]{31, 486, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		/**
		 * MenuBar
		 */
		JMenuBar menuBar = new JMenuBar();
		GridBagConstraints gbc_menuBar = new GridBagConstraints();
		gbc_menuBar.fill = GridBagConstraints.BOTH;
		gbc_menuBar.gridx = 0;
		gbc_menuBar.gridy = 0;
		getContentPane().add(menuBar, gbc_menuBar);
		
		JMenu mnKb = new JMenu("Kwalificatie Beheer");
		menuBar.add(mnKb);
		
		JMenuItem mntmKd = new JMenuItem("Kwalificatie Dossiers");
		mntmKd.setActionCommand("mntmKd");
		mntmKd.addActionListener( new MainFrameActions(this));
		mnKb.add(mntmKd);
		
		/*JMenuItem mntmWp = new JMenuItem("Werkprocessen");
		mntmWp.setActionCommand("mntmWp");
		mntmWp.addActionListener( new MainFrameActions(this));
		mnKb.add(mntmWp);
		mntmWp.setActionCommand("mntmBk");
		mntmWp.addActionListener( new MainFrameActions(this));
		
		JMenu mnGebruikersbeheer = new JMenu("Gebruikersbeheer");
		menuBar.add(mnGebruikersbeheer);
		
		JMenuItem mntmkd = new JMenuItem("bewijskaart");
		menuBar.add(mntmkd);*/
		
		/**
		 * MainPanel
		 */
		mainPanel = new JPanel();
		GridBagConstraints gbcMainPanel = new GridBagConstraints();
		gbcMainPanel.fill = GridBagConstraints.BOTH;
		gbcMainPanel.insets = new Insets(0, 0, 5, 0);
		gbcMainPanel.gridx = 0;
		gbcMainPanel.gridy = 1;
		getContentPane().add(mainPanel, gbcMainPanel);
		GridBagLayout gbl_mainPanel = new GridBagLayout();
		gbl_mainPanel.columnWidths = new int[]{0};
		gbl_mainPanel.rowHeights = new int[]{0};
		gbl_mainPanel.columnWeights = new double[]{Double.MIN_VALUE};
		gbl_mainPanel.rowWeights = new double[]{Double.MIN_VALUE};
		mainPanel.setLayout(gbl_mainPanel);
		/**
		 * Console
		 */
		if(console){
			consolePanel = new ConsolePanel();
			consolePanel.setVisible(true);
			TextArea cpText = consolePanel.aTextArea;
			GridBagConstraints gbc_cp = new GridBagConstraints();
			gbc_cp.fill = GridBagConstraints.BOTH;
			gbc_cp.gridx = 0;
			gbc_cp.gridy = 2;
			cpText.setSize(WIDTH, HEIGHT);
			getContentPane().add(consolePanel, gbc_cp);
		}
		getContentPane().validate();
	}
	
	public void switchMainPanel(Object o){
		mainPanel.removeAll();
		GridBagConstraints gbcMainPanel = new GridBagConstraints();
		gbcMainPanel.fill = GridBagConstraints.BOTH;
		gbcMainPanel.gridx = 0;
		gbcMainPanel.gridy = 0;
		mainPanel.add((JPanel) o, gbcMainPanel);
		mainPanel.revalidate();
	}
}
